package com.sc.nft.service.impl;

import com.sc.nft.enums.OperateUserWalletEnum;
import com.sc.nft.exception.GlobalRunTimeException;
import com.sc.nft.service.UserWalletAdminBatchService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;
@Component
@Slf4j
public class OperateUserWalletBatchFactory implements ApplicationContextAware {
    public Map<OperateUserWalletEnum, UserWalletAdminBatchService> userBalanceTypeEnumUserWalletAdminBatch = new HashedMap();

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        Map<String, UserWalletAdminBatchService> map = applicationContext.getBeansOfType(UserWalletAdminBatchService.class);
        userBalanceTypeEnumUserWalletAdminBatch = new HashMap<>();
        map.forEach((key, value) -> userBalanceTypeEnumUserWalletAdminBatch.put(value.code(), value));
    }

    public <T extends UserWalletAdminBatchService> T getUserWalletAdminBatch(OperateUserWalletEnum code) {
        UserWalletAdminBatchService a = userBalanceTypeEnumUserWalletAdminBatch.get(code);
        if (a != null) {
            return (T) userBalanceTypeEnumUserWalletAdminBatch.get(code);
        }
        throw new GlobalRunTimeException("请求类型参数异常");
    }
}
